package com.cheng.student.controller.servlet;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "FindPrimeServlet", value = "/FindPrimeServlet")
public class FindPrimeServlet
        extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        String startS = request.getParameter("start");
        String endS = request.getParameter("end");
        int start = 0;
        int end = 0;
        try
        {
            start = Integer.parseInt(startS);
            end = Integer.parseInt(endS);
        }
        catch (NumberFormatException e)
        {
            response.getWriter().write("不能为空且只能输入纯数字且只能为整数");
            return;
        }
        int numberOfPrime = 0;
        boolean isPrime = true;
        for (int i = start;i <= end;i++)
        {
            for (int j = 2;j < i;j++)
            {
                if (i % j == 0)
                {
                    isPrime = false;
                }
            }
            if (i == 0 || i == 1)
            {
                isPrime = false;
            }
            if (isPrime)
            {
                numberOfPrime += 1;
            }
            isPrime = true;
        }
        response.getWriter().write("有" + numberOfPrime + "个质数");
    }
}
